package cn.anson.jujuapp.activity;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.ShareActionProvider;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;

import cn.anson.jujuapp.R;
import cn.anson.jujuapp.Utils;
import cn.anson.jujuapp.services.MyService;
/**
添加了忽略的文件
*/

public class MainActivity extends AppCompatActivity implements AMapLocationListener, OnClickListener {

    private AMapLocationClient       locationClient = null;
    private AMapLocationClientOption locationOption = null;
    private Button              button;
    private TextView            tv_result;
    private Button              button_cast;
    private String              result;
    private Toolbar             toolbar;
    private ShareActionProvider mShareActionProvider;
    private String TAG = "MainActivity";
    private Button actionbar_bt;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /*
        判断是否第一次开启activity,
        是，开启服务去定位
        不是，不用去定位
         */
        //开启定位的服务
        Intent intent = new Intent(Utils.getContext(), MyService.class);
        startService(intent);
        setContentView(R.layout.activity_main);
        //1.显示加载进度
        //2.获取数据成功，通知UI线程修改。显示数据
        //初始化定位参数
        //initLocation();
        //获取控件
        initView();


    }

    private void initLocation() {
        //初始化定位的类
        locationClient = new AMapLocationClient(Utils.getContext());
        locationOption = new AMapLocationClientOption();
        // 设置定位模式为低功耗模式
        locationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving);
        // 设置定位监听
        locationClient.setLocationListener(this);

        //初始化定位参数
        initOption();

        // 设置定位参数
        locationClient.setLocationOption(locationOption);
        // 启动定位
        locationClient.startLocation();
        handler.sendEmptyMessage(Utils.MSG_LOCATION_START);
    }


    private void initView() {
        button = (Button) findViewById(R.id.location_bt);
        button_cast = (Button) findViewById(R.id.bluetooth_bt);
        actionbar_bt = (Button) findViewById(R.id.actionbar_bt);
        tv_result = (TextView) findViewById(R.id.tv_result);

        toolbar = (Toolbar) findViewById(R.id.toolbar);

        toolbar.setTitle(R.string.app_name);
        toolbar.setLogo(R.mipmap.ic_launcher);
        setSupportActionBar(toolbar);

        //设置按钮的点击事件
        button.setOnClickListener(this);
        button_cast.setOnClickListener(this);
        actionbar_bt.setOnClickListener(this);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        /* ShareActionProvider配置 */
        mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(menu
                .findItem(R.id.action_share));
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/*");
        mShareActionProvider.setShareIntent(intent);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        return super.onOptionsItemSelected(item);
    }


    /**
     * 定位的结果回调
     *
     * @param aMapLocation
     */
    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        if (aMapLocation != null) {
            //获得消息对象
            Message msg = handler.obtainMessage();
            msg.obj = aMapLocation;
            msg.what = Utils.MSG_LOCATION_FINISH;
            handler.sendMessage(msg);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.location_bt:
                if (button.getText().equals(
                        "开始定位")) {
                    //初始化定位参数
                    initOption();
                    button.setText("停止定位");
                    // 设置定位参数
                    locationClient.setLocationOption(locationOption);
                    // 启动定位
                    locationClient.startLocation();
                    handler.sendEmptyMessage(Utils.MSG_LOCATION_START);
                } else {

                    button.setText("开始定位");
                    // 停止定位
                    locationClient.stopLocation();
                    handler.sendEmptyMessage(Utils.MSG_LOCATION_STOP);
                }
                break;
            case R.id.bluetooth_bt://发送广播
                //定义一个Intent对象
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                if (Utils.getCityName() != null) {
                    intent.putExtra("city", Utils.getCityName());
                    intent.putExtra("districtName", Utils.getDistrictName());
                } else {
                    intent.putExtra("city", "定位失败");
                }

                startActivity(intent);

                /*intent.setAction("cn.anson.jujuapp.broadcast");
                if (result!= null){
                    intent.putExtra("key",result);
                }
                //发送广播
                sendBroadcast(intent);*/
                break;
            case R.id.actionbar_bt:
                MainActivity.this.finish();
                break;
            default:
                break;
        }

    }

    //设置定位的参数
    private void initOption() {
        // 设置是否需要显示地址信息
        locationOption.setNeedAddress(true);
        /**
         *  设置发送定位请求的时间间隔,最小值为1000，如果小于1000，按照1000算
         *  只有持续定位设置定位间隔才有效，单次定位无效
         */
        locationOption.setInterval(2000);
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case Utils.MSG_LOCATION_START:
                    tv_result.setText("正在定位...");
                    break;
                //定位完成
                case Utils.MSG_LOCATION_FINISH:
                    AMapLocation loc = (AMapLocation) msg.obj;
                    result = Utils.getLocationStr(loc);
                    tv_result.setTextSize(30);
                    tv_result.setText(result);
                    break;
                case Utils.MSG_LOCATION_STOP:
                    tv_result.setText("定位停止");
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, "onStart ----------------");
    }


    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume ----------------");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d(TAG, "onRestart  重绘了activity");

    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG, "onPause  点击的了返回的按键");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG, "onStop -------");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy-----------------");
    }
}
